package com.gopay.cashier.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import ocx.GetRandom;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gopay.common.util.PwdControlUtil;

@Controller
public class PasswordController extends BaseController{

    /**
     * 获取支付密码的随机码
     * 
     * @param count 获取随机码的个数
     * @return
     */
    @RequestMapping(value = "getPayPwdUUID")
    @ResponseBody
    public String getPayPwdUUID(int count,int safe,String authCode) {
        printRequest();
    	// 返回结果Map
        Map<String, Object> map = new HashMap<String, Object>();
        String sessionAuthCode = (String)getRequest().getSession().getAttribute(KEY_CAPTCHA);
        String authDisplay = getRequest().getParameter("authDisplay");
		if((!IS_CAPTCHA_DISABLED) && StringUtils.equals(authDisplay,"1") && StringUtils.equals(getRequest().getParameter("pwdLogin"), "1") && StringUtils.isNotBlank(sessionAuthCode) && !(sessionAuthCode).equalsIgnoreCase(authCode)){
			map.put("authCodeCheck", "0");
			return JSONObject.fromObject(map).toString();
		}
        getRequest().getSession().removeAttribute(KEY_CAPTCHA);
//    	setData(count, safe, map, getRequest());
    	return JSONObject.fromObject(map).toString();
    }

    /**
     *
     * @param request
     * @return
     */
    @RequestMapping(value="/getSrandNum")
    @ResponseBody
    public String getSrandNum(HttpServletRequest request){
        String  mcryptKey= GetRandom.generateString(32);
        request.getSession().setAttribute("mcrypt_key", mcryptKey);
        logger.info("设置随机因子："+mcryptKey+"；session id="+request.getSession().getId());
        return mcryptKey;
    }
    
//    private void setData(int count,int safe,Map<String, Object> map,HttpServletRequest request){
//    	 for(int i=1;i<=count;i++){
//         	String uuid = null;
//         	if(safe==0){
//         		uuid = PwdControlUtil.createUUID();
//         	}else{
//         		uuid = PwdControlUtil.createUUID(request);
//         	}
//             map.put("code"+i, uuid);
//         }
//    }
}
